# -*- coding: utf-8 -*-

# -------------------------------------------------------------------
# @Time    : 2024/7/3 15:22
# @Author  : lldhsds
# @File    : main.py
# @Description: 测试程序
# -------------------------------------------------------------------
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/')
def index():
    return '''
    <h1>Calculator API</h1>
    <p>Available endpoints:</p>
    <ul>
        <li><code>/add?a=&lt;number&gt;&b=&lt;number&gt;</code>: Add two numbers</li>
        <li><code>/subtract?a=&lt;number&gt;&b=&lt;number&gt;</code>: Subtract two numbers</li>
        <li><code>/multiply?a=&lt;number&gt;&b=&lt;number&gt;</code>: Multiply two numbers</li>
        <li><code>/divide?a=&lt;number&gt;&b=&lt;number&gt;</code>: Divide two numbers</li>
    </ul>
    '''

@app.route('/add', methods=['GET'])
def add():
    a = float(request.args.get('a'))
    b = float(request.args.get('b'))
    result = a + b
    return jsonify(result=result)

@app.route('/subtract', methods=['GET'])
def subtract():
    a = float(request.args.get('a'))
    b = float(request.args.get('b'))
    result = a - b
    return jsonify(result=result)

@app.route('/multiply', methods=['GET'])
def multiply():
    a = float(request.args.get('a'))
    b = float(request.args.get('b'))
    result = a * b
    return jsonify(result=result)

@app.route('/divide', methods=['GET'])
def divide():
    a = float(request.args.get('a'))
    b = float(request.args.get('b'))
    if b == 0:
        return jsonify(error="Cannot divide by zero!"), 400
    result = a / b
    return jsonify(result=result)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
